package com.jn.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jn.entity.account.User;
import com.jn.service.account.AccountService;

/**
 * @ClassName UserAdminController
 * @Description 管理员管理用户的Controller Controller注解声明该类为控制层
 *              RequestMapping注解声明该类可以拦截的请求,如该类可以拦截/admin/user/*的所有请求
 * @author liuxd
 * @date 2013年8月27日 上午10:59:21
 * 
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAdminController {

	/**
	 * @Fields accountService 使用@Autowired注入accountService
	 */
	@Autowired
	private AccountService accountService;

	/**
	 * @Title list
	 * @Description 加载所有用户
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		List<User> users = accountService.getAllUser();
		model.addAttribute("users", users);
		return "account/adminUserList";
	}

	/**
	 * @Title updateForm
	 * @Description 初始化adminUserForm页面数据
	 * 		value值为该方法可以拦截的url地址 如该方法可以拦截/admin/user/update请求
	 * @param id
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("user", accountService.getUser(id));
		return "account/adminUserForm";
	}

	/**
	 * @Title update
	 * @Description 更新用户
	 * @param user
	 * @param redirectAttributes
	 *            用于重定向时向jsp页面传递数据
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("user") User user,
			RedirectAttributes redirectAttributes) {
		accountService.updateUser(user);
		redirectAttributes.addFlashAttribute("message",
				"更新用户" + user.getLoginName() + "成功");
		return "redirect:/admin/user";
	}

	/**
	 * @Title delete
	 * @Description 删除id用户
	 * @param id
	 * @param redirectAttributes
	 *            用于重定向时向jsp页面传递数据
	 * @return
	 * @throws
	 */
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		User user = accountService.getUser(id);
		accountService.deleteUser(id);
		redirectAttributes.addFlashAttribute("message",
				"删除用户" + user.getLoginName() + "成功");
		return "redirect:/admin/user";
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getUser(
			@RequestParam(value = "id", defaultValue = "-1") Long id,
			Model model) {
		if (id != -1) {
			model.addAttribute("user", accountService.getUser(id));
		}
	}
}
